package com.xjnzy.poi;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.Test;

import javax.swing.*;
import java.io.FileOutputStream;

/**
 * @author 沈旭升
 * @date 2018-04-30 10:23
 */
public class PoiStudy {


    public static void main(String[] args) throws Exception{



    }

    @Test
    public void testHSSFWorkbook() throws Exception{
        //创建一个工作簿对象
        HSSFWorkbook hssfWorkbook = new HSSFWorkbook();

        FileOutputStream fos = new FileOutputStream("abc.xls");
        hssfWorkbook.write(fos);
        hssfWorkbook.close();

    }

    @Test
    public void testXSSFWorkbook() throws Exception{
        //第一步:得到工作簿对象
        Workbook xssfWorkbook = new XSSFWorkbook();

        //第二步:根据工作簿对象创建sheet表
        Sheet google = xssfWorkbook.createSheet("google");
        //第三步:根据sheet对象创建row行对象
        Row row = google.createRow(0);
        //第四步:根据行对象来创建单元格
        Cell cell = row.createCell(0);
        cell.setCellValue("google分享");


        //从工作簿对象中创建字体对象
        Font titleFont = xssfWorkbook.createFont();
        titleFont.setFontName("行楷");
        titleFont.setFontHeightInPoints((short)20);
        titleFont.setColor(IndexedColors.BLUE_GREY.getIndex());

        //从工作簿对象中创建单元格样式对象
        CellStyle titleCellStyle = xssfWorkbook.createCellStyle();
        titleCellStyle.setAlignment(HorizontalAlignment.CENTER);
        titleCellStyle.setFont(titleFont);

        cell.setCellStyle(titleCellStyle);



        //单独创建一个合并单元格的范围对象
        CellRangeAddress cellRangeAddress = new CellRangeAddress(0, 0, 0, 9);
        google.addMergedRegion(cellRangeAddress);



        FileOutputStream fos = new FileOutputStream("abc.xlsx");
        xssfWorkbook.write(fos);
        xssfWorkbook.close();
    }

}
